from flask import Blueprint, jsonify, request
from app.err_no import errno_no
from loguru import logger
import json
import traceback
from app.db import dbStallManage, dbStallQuery
from app.db.mongoClient import MongoError

blueprint = Blueprint('pos_manage', __name__, url_prefix='/api/pos')


@blueprint.route('/v1/pos_query', methods=['POST'])
def pos_query():
    '''
    查询点餐机
    '''
    try:
        j_data = json.loads(request.data)
        diningNo = j_data.get('diningNo')
        stallNo = j_data.get('stallNo')
        if diningNo is None or len(diningNo) <= 0 \
            or stallNo is None or len(stallNo) <= 0:
            return jsonify(errno_no.ErrServerParam.data(None))

        res = dbStallQuery.queryPos(diningNo, stallNo)
        if res is False:
            return jsonify(errno_no.ErrServerDocumentNotExist.data(None))

        return jsonify(errno_no.OK.data({'posList': res}))      
    except MongoError as e:
        logger.error(f'操作mongo异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
    except Exception as e:
        logger.error(f'业务异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))

@blueprint.route('/v1/pos_report', methods=['POST'])
def pos_report():
    '''
    档口点餐机信息上报
    '''
    try:
        j_data = json.loads(request.data)
        diningNo = j_data.get('diningNo')
        stallNo = j_data.get('stallNo')
        deviceNo = j_data.get('device_no')
        deviceType = j_data.get('deviceType')
        softVersion = j_data.get('softVersion')
        skj130No = j_data.get('skj130No')
        if diningNo is None or len(diningNo) <= 0 \
            or stallNo is None or len(stallNo) <= 0 \
            or skj130No is None or len(skj130No) <= 0 \
            or deviceNo is None or len(deviceNo) <= 0 \
            or deviceType is None or len(deviceType) <= 0 \
            or softVersion is None or len(softVersion) <= 0 \
            or skj130No is None or len(skj130No) <= 0 :
            return jsonify(errno_no.ErrServerParam.data(None))

        res = dbStallManage.posReport(diningNo, stallNo, deviceNo, deviceType, softVersion, skj130No)
        if res is None:
            return jsonify(errno_no.ErrServerDocumentNotExist.data(None))

        return jsonify(errno_no.OK.data(None))
    except MongoError as e:
        logger.error(f'操作mongo异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
    except Exception as e:
        logger.error(f'业务异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))


@blueprint.route('/v1/pos_setUpdateStatus', methods=['POST'])
def pos_setUpdateStatus():
    '''
    点餐机升级设置
    '''
    try:
        j_data = json.loads(request.data)
        diningNo = j_data.get('diningNo')
        stallNo = j_data.get('stallNo')
        deviceNo = j_data.get('device_no')
        updateStatus = j_data.get('updateStatus')
        
        if diningNo is None or len(diningNo) <= 0 \
            or stallNo is None or len(stallNo) <= 0 \
            or deviceNo is None or len(deviceNo) <= 0 \
            or updateStatus is None or len(updateStatus) <= 0 :
            return jsonify(errno_no.ErrServerParam.data(None))

        res = dbStallManage.posSetUpdateStatus(diningNo, stallNo, deviceNo, updateStatus)
        if res is None:
            return jsonify(errno_no.ErrServerDocumentNotExist.data(None))

        return jsonify(errno_no.OK.data(None))
    except MongoError as e:
        logger.error(f'操作mongo异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
    except Exception as e:
        logger.error(f'业务异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))